<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="告警类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择告警类型" clearable style="width:120px">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.id"
            :label="dict.alarmName"
            :value="dict.alarmCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推送方式" prop="pushType">
        <el-select v-model="queryParams.pushType" placeholder="请选择推送方式" clearable style="width:120px">
          <el-option
            v-for="dict in alarm_push_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推送对象" prop="pushTarget">
        <el-select v-model="queryParams.pushTarget" placeholder="请选择推送对象" clearable style="width:120px">
          <el-option
            v-for="dict in alarm_push_target"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['alarm:push:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:push:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['alarm:push:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pushList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="告警类型代码" align="center" prop="alarmType" />
      <el-table-column label="告警类型名称" align="center" prop="alarmTypeName" />
      <el-table-column label="推送方式" align="center" prop="pushType">
        <template #default="scope">
          <dict-tag :options="alarm_push_type" :value="scope.row.pushType"/>
        </template>
      </el-table-column>
      <el-table-column label="推送对象" align="center" prop="pushTarget">
        <template #default="scope">
          <dict-tag :options="alarm_push_target" :value="scope.row.pushTarget"/>
        </template>
      </el-table-column>
      <!--
      <el-table-column label="推送用户" align="center" prop="pushUser" />
      -->
      <el-table-column label="规则说明" align="center" prop="comment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['alarm:push:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['alarm:push:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改告警推送对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="pushRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="告警类型" prop="alarmType">
        <el-select v-model="form.alarmType" placeholder="请选择告警类型" clearable>
          <el-option
            v-for="dict in typeOptions"
            :key="dict.id"
            :label="dict.alarmName"
            :value="dict.alarmCode"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="推送方式" prop="pushType">
          <el-select v-model="form.pushType" placeholder="请选择推送方式">
            <el-option
              v-for="dict in alarm_push_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推送对象" prop="pushTarget">
          <el-select v-model="form.pushTarget" placeholder="请选择推送对象">
            <el-option
              v-for="dict in alarm_push_target"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="推送部门" prop="pushUser" v-if="form.pushTarget = 'dept'">
          <ds-tree dsCode="sys_dept_tree" v-model="form.pushUser" />
        </el-form-item>
        <el-form-item label="推送岗位" prop="pushUser" v-else-if="form.pushTarget = 'post'">
          <ds-select dsCode="sys_post_select" formType="mult" v-model="form.pushUser" />
        </el-form-item>
        <el-form-item label="推送角色" prop="pushUser" v-else-if="form.pushTarget = 'role'">
          <ds-select dsCode="sys_role_select" formType="mult" v-model="form.pushUser" />
        </el-form-item>
        <el-form-item label="推送用户" prop="pushUser" v-else-if="form.pushTarget = 'user'">
          <ds-select dsCode="sys_user_select" formType="mult" v-model="form.pushUser" />
        </el-form-item>
        <el-form-item label="规则说明" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入规则说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>

<script setup name="Push">
import { listPush, getPush, delPush, addPush, updatePush } from "@/api/alarm/push";
import { selectType } from "@/api/alarm/type";
import DsSelect from '@/components/FormInput/Select';
import DsTree from '@/components/FormInput/Tree';

const { proxy } = getCurrentInstance();
const { alarm_push_target, alarm_push_type } = proxy.useDict('alarm_push_target', 'alarm_push_type');

const pushList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const typeOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'desc',
    alarmType: null,
    pushType: null,
    pushTarget: null,
    pushUser: null,
  },
  rules: {
    alarmType: [
      { required: true, message: "告警类型不能为空", trigger: "blur" }
    ],
    pushType: [
      { required: true, message: "推送方式不能为空", trigger: "change" }
    ],
    pushTarget: [
      { required: true, message: "推送对象不能为空", trigger: "change" }
    ],
    pushUser: [
      { required: true, message: "推送用户不能为空", trigger: "blur" }
    ],
    comment: [
      { required: true, message: "规则说明不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询告警推送列表 */
function getList() {
  loading.value = true;
  listPush(queryParams.value).then(response => {
    pushList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    alarmType: null,
    pushType: null,
    pushTarget: 'user',
    pushUser: null,
    comment: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("pushRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加告警推送";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPush(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改告警推送";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pushRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePush(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPush(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除选中的告警推送数据项？').then(function() {
    return delPush(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('alarm/push/export', {
    ...queryParams.value
  }, `push_${new Date().getTime()}.xlsx`)
}


getList();
selectType().then(response=>{
  typeOptions.value = response.rows;
})
</script>
